var class_num=location.search;
class_num=class_num.split('?')[1];
$('.class_num').text(class_num);
if (islogin()) {
    $.post({
        url: '../../requests/teacher/select_section.php',
        data: {'class': class_num},
        async: false,
        success: function (data) {
            for (var i = 0; i < data.length; i++) {
                var dom1 = "<option value='"+data[i].num+"'>"+data[i].num+"."+data[i].name+"</option>";
                $("select[name='sec']").append(dom1);
            }
        }
    });

    $("select[name='sec']").change(function () {
        if ($(this).val()!="空") {
            $("select[name='sub']").find('option').remove();
            $.post({
                url:'../../requests/teacher/select_subsection.php',
                data:{'class':class_num,'sec': $("select[name='sec']").val()},
                async:false,
                success:function (data) {
                    $("select[name='sub']").append("<option value=''>空</option>");
                    for (var i = 0; i < data.length; i++) {
                        var dom1 = "<option value='"+data[i].num+"'>"+data[i].num+"."+data[i].name+"</option>";
                        $("select[name='sub']").append(dom1);
                    }
                }
            });
        }
        else if ($(this).val() == "空") {
            $("select[name='sub']").find('option').remove();
            $("select[name='sub']").append("<option value=''>空</option>");
        }
    });

    $('#btn_add').click(function () {
        if (!check_val()) {
            $$.alert('参数缺失！');
        }
        else {
            var fd=new FormData($('#add_courseware_form')[0]);
            fd.append('class',class_num);
            $.post({
                url:'../../requests/teacher/add_courseware.php',
                data:fd,
                processData: false,
                contentType: false,
                success:function (data) {
                    if (data == '1'){
                        $$.alert('添加成功！',function () {
                            window.history.go(-1);
                        });

                    }
                    else if (data == '0') {
                        $$.alert('添加失败!',function () {
                            window.history.go(-1);
                        });
                    }
                    else if (data == 'file_exist') {
                        $$.alert('添加失败：文件名已存在！');
                    }
                    else {
                        window.location.href='../../index.html';
                    }
                }
            });
        }
    });

}
else window.location.href='../../index.html';

function check_val() {
    if ($("input[name='attach']").val()==""||$('.class_num').text()=="") {
        return false;
    }
    else return true;

}